package Web;



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wuhan
 * @date 2019/10/17 15:07
 */





public class TcoDemo2 {
    public static void main(String[] args) {

    }
}

/**
 * 1.建立socket服务，指定要连接的主机和端口。
 * 2.获取socket流中的输出流，将数据写到输出流中，通过网络发送给服务端。
 * 3.获取socket中的输入流，将服务器反馈的数据输入流读取到，并打印
 * 4.关闭资源
 */
class TcpClient2{
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("172.20.10.11",10010);
        OutputStream outputStream = s.getOutputStream();
        outputStream.write("Hello Sever !".getBytes());
        InputStream inputStream = s.getInputStream();
        byte[] buf = new byte[1024];
        int len = inputStream.read(buf);
        System.out.println(new String(buf,0,len));
        s.close();
    }
}

class TcpServer{
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = new ServerSocket(10010);
        Socket s = ss.accept();
        String IP = s.getInetAddress().getHostAddress();
        System.out.println("IP:"+IP);
        InputStream inputStream = s.getInputStream();
        byte[] buf = new byte[1024];
        int len = inputStream.read(buf);
        System.out.println(new String(buf,0,len));
        Thread.sleep(10000);
        OutputStream outputStream = s.getOutputStream();
        outputStream.write("哥们收到，你也好！".getBytes());
        s.close();
        ss.close();


    }
}
